function getGridOptions() {
    return {
        sortable: true,
        groupable: false,
        selectable: false,
        editable: false,
        resizable: true,
        reorderable: true,
        pageable: {refresh: true, pageSize: 20, pageSizes: [20, 50, 100, 200, 500]}
    }
}

function modal(title, url, options) {
    var settings = $.extend({
        width: 600,
        minWidth: 450,
        height: 400,
        minHeight: 400,
        modal: true,
        resizeable: true,
        title: title,
        content: url
    }, options);
    var modalElement = $('<div></div>').appendTo(document.body);
    var modalObject = modalElement.kendoWindow(settings).data("kendoWindow");
    modalObject.center();
    modalObject.bind("close", function () {
        modalObject.destroy();
    });
    modalElement.keydown(function (event) {
        if (event.keyCode == 27) {
            modalObject.close();
        }
    });
    return modalElement;
}

function getDataSource(idField, readUrl, filter) {
    return new kendo.data.DataSource(getDataSourceConfig(idField, readUrl, filter))
}

function getDataSourceConfig(idField, readUrl, filter) {
    return {
        schema: {
            model: {id: idField}, data: function (response) {
                return response.content || response.data || response
            }, total: 'totalElements'
        }, transport: {
            parameterMap: function (options) {
                return kendo.stringify(options)
            }, read: getTransport(readUrl)
        }, pageSize: 20, serverPaging: true, serverFiltering: true, serverSorting: true, filter: filter
    }
}

function getDataListSourceConfig(idField, readUrl, filter) {
    return {
        schema: {
            model: {id: idField}, data: function (response) {
                return response.data || response
            }
        }, transport: {
            parameterMap: function (options) {
                return kendo.stringify(options)
            }, read: getTransport(readUrl)
        }, serverFiltering: false, filter: filter
    }
}

function bindHasChildren(data, childrenField) {
    $.each(data, function (i, item) {
        if (childrenField) {
            item.items = item[childrenField];
            item["children"] = null;
        }
        item.hasChildren = item.items && item.items.length > 0;
        if (item.hasChildren) {
            bindHasChildren(item.items, childrenField)
        }
    })
}

function getDataListSource(idField, readUrl, filter) {
    return new kendo.data.DataSource(getDataListSourceConfig(idField, readUrl, filter))
}

function getTransport(url, type, dataType, contentType) {
    return {
        contentType: contentType || 'application/json',
        dataType: dataType || 'json',
        type: type || 'POST',
        url: url
    }
}

function getHierarchicalDataSource(idField, readUrl, filter, children, hasChildren, expanded) {
    var config = {
        methods: 'get',
        schema: {
            model: {id: idField}, data: function (response) {
                var list = response.data || response;
                bindHasChildren(list, children);
                return list
            }
        }, transport: {
            parameterMap: function (options) {
                return kendo.stringify(options)
            }, read: getTransport(readUrl)
        }, serverFiltering: true, filter: filter
    };
    if (expanded == undefined) {
        expanded = true
    }
    config.schema.model.expanded = expanded;
    if (children) config.schema.model.children = children;
    if (hasChildren) config.schema.model.hasChildren = hasChildren;
    return new kendo.data.HierarchicalDataSource(config)
}

function getTreeListDataSource(idField, readUrl, parentField, expanded) {
    if (expanded == undefined) {
        expanded = true
    }
    var datasource = {
        schema: {
            model: {
                id: idField,
                fields: {parentId: {field: parentField, nullable: true}},
                expanded: expanded
            }, data: function (response) {
                return response.data || response
            }
        }, transport: {
            parameterMap: function (options) {
                return kendo.stringify(options)
            }, read: getTransport(readUrl)
        }, serverFiltering: true, serverSorting: true
    };
    return new kendo.data.TreeListDataSource(datasource)
}

function getComboDatasource(url) {
    return new kendo.data.DataSource({
        serverFiltering: false,
        transport: {read: {dataType: "json", url: contextPath + url}}
    })
}


$(function () {
    $(document.body).on("click", "a[target='_modal'][href]", function (event) {
        event.preventDefault();
        var element = $(this);
        var title = element.attr("title") || element.text(), url = element.attr("href");
        var options = element.attr("options");
        modal(title, url, eval('(' + options + ')'));
    });

});

Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
        "H+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    var week = {
        "0": "/u65e5",
        "1": "/u4e00",
        "2": "/u4e8c",
        "3": "/u4e09",
        "4": "/u56db",
        "5": "/u4e94",
        "6": "/u516d"
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
};
